package com.atguigu.srb.core.controller;


import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.pojo.vo.InvestVO;
import com.atguigu.srb.core.service.LendItemService;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.util.JwtUtils;
import com.atguigu.srb.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author fun
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/api/core/lendItem")
public class LendItemController {
    @Autowired
    LendItemService lendItemService;
    @Autowired
    TransFlowService transFlowService;

    @PostMapping("/auth/commitInvest")
    public R commitInvest(@RequestBody InvestVO investVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            Long userId = JwtUtils.getUserId(token);
            String userName = JwtUtils.getUserName(token);
            investVO.setInvestUserId(userId);
            investVO.setInvestName(userName);
            //构建充值自动提交表单
            String formStr = lendItemService.commitInvest(investVO);
            return R.ok().data("formStr", formStr);
        }
        return R.error();
    }

    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        String agentBillNo = (String) paramMap.get("agentBillNo");
        //判断交易流水是否存在
        Boolean isSave = transFlowService.isSaveTransFlow(agentBillNo);
        if (isSave) {
            lendItemService.hfbNotify(paramMap);
        }
        return "success";
    }
}

